package com.briup.cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.briup.cms.domain.User;
import com.briup.cms.domain.extend.UserExtend;
import com.briup.cms.exception.ServiceException;
import com.briup.cms.result.ResultCode;
import com.briup.cms.service.UserService;
import com.briup.cms.mapper.UserMapper;
import com.briup.cms.utils.JwtUtil;
import com.briup.cms.utils.Md5Util;
import com.briup.cms.vo.LoginParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 *
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Autowired
    UserMapper userMapper;

    @Override
    public String login(LoginParam loginParam) {
        // 使用mp的内置方法做条件查询
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getUsername,loginParam.getUsername());
        lqw.eq(User::getPassword,Md5Util.MD5(loginParam.getPassword()));
        User user = userMapper.selectOne(lqw);
        // 判断user是否存在
        if(Objects.isNull(user)){
            log.error("登陆失败，账号密码验证不通过");
            throw new ServiceException(ResultCode.LOGIN_FAIL);
        }
        // 存在的话生成token
        Map<String,Object> claims = new HashMap<>();
        claims.put("userId",user.getId());
        claims.put("username",user.getUsername());
        return JwtUtil.generateJwt(claims);
    }

    @Override
    public void addUser(User user) {
        // 记得验证user的用户名的唯一性

        // 密码需要加密存储
        user.setPassword(Md5Util.MD5(user.getPassword()));
        // 注册时间需要加入当前时间
        user.setRegisterTime(LocalDateTime.now());
        // 调用mp提供的单表方法
        save(user);
    }

    @Override
    public User getUserById(Long id) {
        return getById(id);
    }

    @Override
    public void setVipStatus(Long id) {
        // 查出来如果是0，变为1；如果是1，变为0
        User user = getById(id);
        User temp = new User();
        int isVip = user.getIsVip();
        if(isVip == 0){
            temp.setIsVip(1);
        }else {
            temp.setIsVip(0);
        }
        temp.setId(user.getId());
        updateById(temp);
    }

    @Override
    public void editUserInfo(User user) {
        String password = user.getPassword();
        if(StringUtils.hasText(password)){
            user.setPassword(Md5Util.MD5(password));
        }
        boolean b = updateById(user);
        if(!b){
            log.error("用户信息更新失败");
            throw new ServiceException(ResultCode.FAIL);
        }
    }

    @Override
    public void removeBatch(List<Long> ids) {
        if(Objects.isNull(ids)|| ids.isEmpty()){
            log.error("删除用户时，ids为空");
            throw new ServiceException(ResultCode.FAIL);
        }
        if(ids.size() == 1){
            removeById(ids.get(0));
        }else {
            removeByIds(ids);
        }
    }

    @Override
    public IPage<UserExtend> getAllWithRoleByPage(Integer pageNum, Integer pageSize, Integer isVip, Integer roleId, String status, String username) {
        IPage<UserExtend> page = new Page<>(pageNum,pageSize);
        return userMapper.selectAllWithRole(page, username, status, roleId, isVip);
    }
}




